<?php
session_start ();
if (! isset ( $_SESSION ['id'] ) or ! isset ( $_GET ['tname'] ))
	exit ();
require_once 'common.inc';
require_once 'PHPExcel/Classes/PHPExcel.php';

// 检查权限
$sql = "";
if ($_SESSION ['aboutme'] != '总部用户') {
	$bRight = true;
	switch ($_GET ['tname']) {
		case 'customer' :
		case 'logic' :
		case 'output' :
			$sql = "SELECT * FROM " . $_GET ['tname'] . " WHERE tmncode in (SELECT tmncode FROM management WHERE id='" . $_SESSION ['id'] . "')";
			break;
		case 'user' :
		case 'management' :
			$sql = "SELECT * FROM " . $_GET ['tname'] . " WHERE id='" . $_SESSION ['id'] . "'";
			break;
		case 'item' :
		case 'input' :
			$sql = "SELECT * FROM " . $_GET ['tname'];
			break;
		default :
			// 不能导出
			$bRight = false;
			break;
	}
	if (! $bRight) {
		echo ("{success:false,msg:\"导出未授权的数据！\"}");
		exit ();
	}
} else {
	$sql = "SELECT * FROM " . $_GET ['tname'];
}

// 开始生成excel文件
// Create new PHPExcel object
$objPHPExcel = new PHPExcel ();

// Set document properties
$objPHPExcel->getProperties ()->setCreator ( "" )->setLastModifiedBy ( "" )->setTitle ( "" );
// 获取元数据并写入
$result = execSQL ( "SHOW FULL COLUMNS FROM " . $_GET ['tname'] );
for($i = 0; $row = mysql_fetch_array ( $result ); $i ++) {
	$objPHPExcel->setActiveSheetIndex ( 0 )->setCellValueByColumnAndRow ( $i, 1, $row ['Comment'] . "(" . $row ['Field'] . ")" );
}

// 获取数据并写入
if (isset ( $_GET ['json'] ) && $_GET ['json'] != "") {
	$json = json_decode ( $_GET ['json'] );
	$str = "";
	for($i = 0; $i < count ( $json ); $i ++) {
		$str .= "(";
		foreach ( $json [$i] as $key => $val ) {
			$str .= $key . "='" . addslashes ( $val ) . "' AND ";
		}
		$str = substr ( $str, 0, - 5 ) . ") OR ";
	}
	if ($str != "") {
		if (strpos ( $sql, "WHERE" ) === false)
			$sql .= " WHERE " . substr ( $str, 0, - 4 );
		else
			$sql .= " AND (" . substr ( $str, 0, - 4 ) . ")";
	}
}
$result = execSQL ( $sql );
for($i = 2; $row = mysql_fetch_row ( $result ); $i ++) {
	for($j = 0; $j < count ( $row ); $j ++) {
		$objPHPExcel->setActiveSheetIndex ( 0 )->setCellValueByColumnAndRow ( $j, $i, $row [$j] );
	}
}

// Rename worksheet
$objPHPExcel->getActiveSheet ()->setTitle ( $_GET ['tname'] );

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex ( 0 );

// Redirect output to a client’s web browser (Excel5)
header ( 'Content-Type: application/vnd.ms-excel' );
header ( 'Content-Disposition: attachment;filename="' . $_GET ['tname'] . '.xls"' );
header ( 'Cache-Control: max-age=0' );
// If you're serving to IE 9, then the following may be needed
header ( 'Cache-Control: max-age=1' );

// If you're serving to IE over SSL, then the following may be needed
header ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); // Date in the past
header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' ); // always modified
header ( 'Cache-Control: cache, must-revalidate' ); // HTTP/1.1
header ( 'Pragma: public' ); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
$objWriter->save ( 'php://output' );
exit ();
?>

